package de.karlNet.gifSocket;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;

public class Screenshotter {
	private final ScreenshotBuffer screenshotBuffer;
	private final Robot robot;
	Rectangle captureSize = new Rectangle(Toolkit.getDefaultToolkit()
			.getScreenSize());

	public Screenshotter(ScreenshotBuffer screenshotBuffer) throws AWTException {
		this.screenshotBuffer = screenshotBuffer;
		this.robot = new Robot();
		this.run();
		
	}

	public void run() {
		System.out.println("started screenshotter");
		new Thread(new Runnable() {

			public void run() {
					while (Thread.currentThread().isAlive()) {
						getScreenshot();
					}
			}
		}).start();
		
//		int i=0;
//		while (Thread.currentThread().isAlive()) {
//			long start = System.currentTimeMillis();
//			BufferedImage bufferedImage = screenshotBuffer.getImage(i);
//			animatedGifEncoder.addFrame(bufferedImage);
//			i++;
//			System.out.println("took: " + Math.abs(start-System.currentTimeMillis()));
//		}
	}

	private void getScreenshot() {
		final BufferedImage bufferedImage = (BufferedImage) robot.createScreenCapture(captureSize);
		this.screenshotBuffer.addScreenshot(bufferedImage);
		
		
	}

}
